package com.qf.controller;

import com.qf.pojo.Expenditure;
import com.qf.service.ExpenditureService;
import com.qf.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/expenditure")
@CrossOrigin
public class ExpenditureController {
    @Autowired
    ExpenditureService expenditureService;

    @GetMapping("/findAll")
    public ResultMsg findAll(@RequestParam(required = false, defaultValue = "1") Integer current, @RequestParam(required = false, defaultValue = "20") Integer pageSize) {
        return expenditureService.findAll(current, pageSize);
    }

    @GetMapping("/findOne")
    public ResultMsg findAll(Integer expenditureId) {
        return expenditureService.findOne(expenditureId);
    }

    @GetMapping("/findAllByCondition")
    public ResultMsg findAllByCondition(@RequestParam(required = false, defaultValue = "") String startTime,
                                        @RequestParam(required = false, defaultValue = "") String endTime,
                                        @RequestParam(required = false, defaultValue = "") String expenditureFunctionary,
                                        @RequestParam(required = false, defaultValue = "") String expenditureType,
                                        @RequestParam(required = false, defaultValue = "") String paymentMethod,
                                        @RequestParam(required = false, defaultValue = "1") Integer current,
                                        @RequestParam(required = false, defaultValue = "20") Integer pageSize) {
        return expenditureService.findAllByCondition(startTime, endTime, expenditureFunctionary, expenditureType, paymentMethod, current, pageSize);
    }

    @PostMapping("/add")
    public ResultMsg add(@RequestBody Expenditure expenditure) {
        return expenditureService.addExpenditure(expenditure);
    }

    @PostMapping("/update")
    public ResultMsg update(@RequestBody Expenditure expenditure) {
        return expenditureService.modifyExpenditure(expenditure);
    }

    @PostMapping("/delete")
    public ResultMsg delete(@RequestBody Map<String, Integer> map) {
        return expenditureService.deleteExpenditure(map.get("expenditureId"));
    }
}
